// Profile.cpp

#include "stdafx.h"

#include <fstream>
#include "tinyxml/tinyxml.h"

#include "Profile.h"

CProfile::CProfile(void) :
m_pXMLDoc(NULL), m_pRoot(NULL), m_bLocked(false)
{
}

CProfile::~CProfile()
{

}

//////////////////////////////////////////////////////////////////////////
// Load & clear
void CProfile::Load(std::string strXMLFile)
{
	m_bLocked = true;

	m_pXMLDoc = new TiXmlDocument(strXMLFile.c_str());

	std::ifstream ifs(strXMLFile.c_str());

	char buffer[2048];
	char c, *p = buffer;
	while(ifs.get(c)) { *p++=c; }
	*p = 0;
	ifs.close();

	if(!m_pXMLDoc->Parse(buffer))
	{
		::OutputDebugStringA(m_pXMLDoc->ErrorDesc());
		Clear();
		return;
	}

	m_pRoot = m_pXMLDoc->RootElement();
	if (!m_pRoot)
	{
		Clear();
	}
}

void CProfile::Clear(void)
{
	if (m_pXMLDoc)
	{
		delete m_pXMLDoc;
		m_pXMLDoc = NULL;
	}

	m_bLocked = false;
}

//////////////////////////////////////////////////////////////////////////
// get data
int CProfile::GetIntData(const char* pszElem, const char* pszAttrib)
{
	TiXmlNode* child = NULL;
	for (child = m_pRoot->FirstChild(); child; child=child->NextSibling())
	{
		if((child->Type() == TiXmlNode::ELEMENT) && (!strcmp(child->Value(), pszElem)))
		{
			const TiXmlElement *Lev2 = (const TiXmlElement*)child;
			int result = 0;
			return (Lev2->Attribute(pszAttrib, &result)) ? result : 0;
		}
	}

	return 0;
}